#------------------------------------------------------------------
# COMPILER
#
#      GNU Tools 4 ARM
#
#------------------------------------------------------------------
ifeq ($(GL_COMPILER),GNUTCODESOURCERY)
   A_CC         = $(GCC_BIN_CSOURCERY)/arm-none-eabi-gcc
   T_CC         = $(GCC_BIN_CSOURCERY)/arm-none-eabi-gcc -mthumb
   ASM          = $(GCC_BIN_CSOURCERY)/arm-none-eabi-as
   FROMELF      = $(GCC_BIN_CSOURCERY)/arm-none-eabi-objcopy
   OBJDUMP      = $(GCC_BIN_CSOURCERY)/arm-none-eabi-objdump
   LINK         = $(GCC_BIN_CSOURCERY)/arm-none-eabi-gcc
   ARCH         = $(GCC_BIN_CSOURCERY)/arm-none-eabi-ar
   STRIP        = $(GCC_BIN_CSOURCERY)/arm-none-eabi-strip
   CC_DEP       = $(A_CC)
   OFLAG        = -o
   OUTFILEEXT   = elf
   LIBEXT       = a
   INCLUDE_FLAG = -I
   ifndef LINUXGCC
       WIN_PATH = 1
   endif


   ifeq ($(GL_CPUTYPE),CORTEX-M3)
      CPU_FLAG := cortex-m3
      MTHUMB   := -mthumb
   endif
   ifeq ($(GL_CPUTYPE),ARM966E-S)
      CPU_FLAG := arm966e-s
   endif

   ifeq ($(LINKMAP),)
      LINKMAP = linkmap.ld
   endif

   # C compiler options
   OPT_LEVEL   = 2
   C_FLAGS     = -mcpu=$(CPU_FLAG) -mtune=$(CPU_FLAG)
   C_FLAGS    += -O$(OPT_LEVEL)
#   C_FLAGS    += -mstructure-size-boundary=32
   C_FLAGS    += -msoft-float
   C_FLAGS    += $(MTHUMB)
#   C_FLAGS    += -g
   C_FLAGS    += $(TOP_C_FLAGS)
   C_DEFINES   = $(foreach flag,$(CDEFINES) $(TOP_CDEFINES),-D$(flag))
   C_DEFINES  += -DGCC_VERSION="(__GNUC__*10000+__GNUC_MINOR__*100+__GNUC_PATCHLEVEL__)"
   C_FLAGS    += $(C_DEFINES)
   C_C_FLAG    = -c

   # Assembler compiler options
   ASM_FLAGS   = -mcpu=$(CPU_FLAG)
#   ASM_FLAGS  += -g
   ASM_FLAGS  += $(MTHUMB)
   ASM_FLAGS  += $(TOP_ASM_FLAGS)
   ASM_DEFINES = $(foreach flag,$(ASMDEFINES) $(TOP_ASMDEFINES), \
                   --defsym $(flag)=1")
   ASM_FLAGS  += $(ASM_DEFINES)
   ASM_C_FLAG  =

   # Linker options
   LINK_FLAGS  = $(MTHUMB)
   LINK_FLAGS += -msoft-float
   LINK_FLAGS += -mcpu=$(CPU_FLAG)
   LINK_FLAGS += -nostartfiles
   LINK_FLAGS += -Xlinker -static
   LINK_FLAGS += -Xlinker --print-map
   LINK_FLAGS += -Xlinker -Map=$(call CYGPATH,$(OUTPUT_DIR_SLASH)$(OUTDIR)/$(NAME).map)
   LINK_FLAGS += -Xlinker -cref
   LINK_FLAGS += -Xlinker -O$(OPT_LEVEL)
   LINK_FLAGS += -Xlinker --script=$(LINKMAP)
   LINK_FLAGS += -Xlinker --gc-sections
   A_LIBS      = $(A_LIBS_P)

   # Archiver options
   ARCH_OPTS   = -r

   C_DEP_CMD   = -M $(C_FLAGS) -MT$(basename $@).o $(call CYGPATH,$<) > $@
   ASM_DEP_CMD = --MD $(call CYGPATH,$@) $(call CYGPATH,$<) -o $(call CYGPATH,$(basename $@).o)


   # info and output
   RAW_OUTPUT_CMD  = $(FROMELF) -O binary $(call CYGPATH,$(OUTPUT_DIR_SLASH)$(OUTPUT_FILENAME)) \
                                          $(call CYGPATH,$(OUTPUT_DIR_SLASH)$(OUTPUT_RAWFILE)); \
                     $(FROMELF) -O ihex   $(call CYGPATH,$(OUTPUT_DIR_SLASH)$(OUTPUT_FILENAME)) \
                     $(call CYGPATH,$(OUTPUT_DIR_SLASH)$(OUTPUT_HEXFILE))
   INFO_OUTPUT_CMD = $(OBJDUMP) -S -z -D $(call CYGPATH, '$(OUTPUT_DIR_SLASH)$(OUTPUT_FILENAME)') \
                     >$(OUTPUT_DIR_SLASH)$(OUTPUT_INFFILE); \

   INFO_OUTPUT_CMD =

   # no need to fix dependency files, gcc is generating POSIX paths
   # some lines to fix path issue inside depend files
   # i.e change dos path into unix path
   # W: does not like leading spaces in lines!
   ifdef LINUXGCC
      FIX_DEPEND = echo -n
   else
      FIX_DEPEND = mv $@ $@.0; \
                cat $@.0 | grep -v ".o:" | sed 's/^ *//' > $@.1 ;\
                cygpath -u -f $@.1 > $@.2            ;\
                cat $@.2 | sed 's/ \/$$/ \\/' > $@.3 ;\
                echo "$(basename $@).o: \\" > $@           ;\
                cat $@.3 >> $@                       ;\
                rm $@.*
   endif

endif

